﻿using QuanLyNganHangDe.DAL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MarkDocuments
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// thực hiện 1 tác vụ
        /// </summary>
        /// <param name="tagTask"></param>
        public void DoTagging(TagTask tagTask)
        {
            try
            {

            }
            catch (Exception exc)
            {
                tagTask.SetError(exc);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<TagTask> lst = null;
            try
            {
                if (bsConvertTask.DataSource == null)
                    lst = new List<TagTask>();
                else
                    lst = bsConvertTask.DataSource as List<TagTask>;
            }
            catch
            {
                lst = new List<TagTask>();
            }

            var dlg = new OpenFileDialog();
            dlg.Filter = "Tệp DOCX (*.DOCX)|*.docx";
            dlg.Title = "Chon tep ngan hang cau hoi de danh dau...";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var filename in dlg.FileNames)
                {
                    var needToAdd = true;
                    foreach (var task in lst)
                        if (task.InputFilename.ToUpper().Equals(filename))
                        {
                            needToAdd = false;
                            break;
                        }
                    if (needToAdd)
                    {
                        var task = new TagTask(filename);
                        lst.Add(task);
                    }
                }
            }

            bsConvertTask.DataSource = lst;
            lbxConvertTask.DataSource = bsConvertTask;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = bsConvertTask;
            bsConvertTask.ResetBindings(true);
            UpdateGuiByListOfInputFiles();
        }

        private void UpdateGuiBySelectedTTask()
        {
            TagTask selectedTask = null;
            if (bsConvertTask.Current != null)
                selectedTask = bsConvertTask.Current as TagTask;
            if (selectedTask == null)
            {
                btnRemoveFile.Enabled = false;
                tbxConvertTask.Text = "";
            }
            else
            {
                btnRemoveFile.Enabled = true;
                tbxConvertTask.Text = selectedTask.Description;
            }
        }

        private void bsConvertTask_CurrentChanged(object sender, EventArgs e)
        {
            UpdateGuiBySelectedTTask();
        }

        private void btnRemoveFile_Click(object sender, EventArgs e)
        {
            if (bsConvertTask.Current != null)
            {
                bsConvertTask.RemoveCurrent();

                lbxConvertTask.Focus();
                UpdateGuiBySelectedTTask();
                UpdateGuiByListOfInputFiles();
            }
        }

        private void UpdateGuiByListOfInputFiles()
        {
            if (bsConvertTask.Count > 0)
            {
                lblTepDauVao.Text = string.Format("{0} tệp", bsConvertTask.Count);
                btnDoTagging.Enabled = true;
            }
            else
            {
                lblTepDauVao.Text = "";
                btnDoTagging.Enabled = false;
            }
        }

        private void FormDoTagging_Load(object sender, EventArgs e)
        {
            bsConvertTask.DataSource = null;
            UpdateGuiByListOfInputFiles();
            UpdateGuiBySelectedTTask();
        }

        private void btnDoTagging_Click(object sender, EventArgs e)
        {
            // Xác định danh sách
            List<TagTask> lst = null;
            try
            {
                lst = bsConvertTask.DataSource as List<TagTask>;
            }
            catch
            {
                MessageBox.Show("Không xác định được danh sách các tài liệu");
                return;
            }

            var frm = new FormDoTagging(lst);
            frm.ShowDialog(this);
            bsConvertTask.ResetCurrentItem();
            UpdateGuiBySelectedTTask();
        }
    }
}
